Skip to content

[#707] Issue the remember-me token only after the OTP, and renew the session ID on every sign-in - #713

Open
armanist wants to merge 4 commits into
quantum-php:3.2.xfrom
armanist:issue/707-remember-me-2fa
Open

armanist wants to merge 4 commits into
quantum-php:3.2.xfrom
armanist:issue/707-remember-me-2fa

Conversation

@armanist

@armanist armanist commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #707

What

With two-factor authentication on, SessionAuthAdapter issues the remember-me token and cookie only after the OTP is verified, and every sign-in gets a new session ID.

  • signin() with two-factor on issues no remember token. It records in the session of the browser that signed in that "remember me" was asked for; a sign-in without it, and signout(), remove that record.
  • verifyOtp() issues the remember token and cookie when the record matches the verified user, removes the record, and regenerates the session ID before it signs the user in.
  • A sign-in through the remember cookie in user() also regenerates the session ID.
  • With two-factor off, sign-in and remember-me work as before. The JWT adapter is not changed.

Why

  • signin() issued the remember token before the OTP step, and checkRememberToken() refused the cookie only while an OTP was pending. When the owner then completed an OTP sign-in in another browser, the pending OTP was cleared and the cookie of the unverified sign-in signed in without its OTP. A failure while storing the OTP left a working remember token at once.
  • verifyOtp() and the remember-cookie sign-in kept the session ID the browser had before, while signin() without two-factor regenerates it, so a session ID planted before such a sign-in stayed valid for the signed-in user.

Changed paths

  • src/Auth/Adapters/SessionAuthAdapter.php
  • tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php
  • CHANGELOG.md

Testing

New tests that fail without the change:

  • a remember sign-in with two-factor on sets no cookie before the OTP (a cookie was set);
  • the cookie of a sign-in whose OTP was never entered does not sign in after the owner verifies an OTP elsewhere (it signed in), and no remember token is stored;
  • when storing the OTP fails, no remember token is stored (one was);
  • a later sign-in without "remember me", also one whose OTP mail fails, and signout() before the OTP leave no cookie (the earlier request was applied);
  • when storing the remember token fails in verifyOtp(), no remember request is left in the session;
  • verifyOtp() and a remember-cookie sign-in change the session ID (it stayed the same), keep the session data, and a later user() keeps the new ID.

Tests that pass before and after: remember-me after a verified OTP and after resendOtp(), no cookie without "remember me", remember-me with two-factor off, and the existing auth tests.

Before and after, on 12 sign-in scenarios run against the old and the new adapter: every scenario that worked before behaves the same; the remember cookie of an unverified sign-in, after a failed OTP store, after a declined later sign-in and after an abandoned sign-in no longer signs in; the session ID changes after the OTP and on a remember-cookie sign-in.

Remember tokens issued before the upgrade are not revoked; the changelog tells applications how to clear them. The starter project needs no change: its web module signs in, verifies and resends in the same browser, and nothing in it reads the session ID or the remember cookie.

Summary by CodeRabbit

  • Security
    • When two-factor authentication is enabled, “Remember me” is applied only after successful OTP verification. Sign-ins without two-factor authentication are unchanged.
    • Sessions are refreshed after OTP verification and when signing in with a remember-me cookie, while preserving session data.
    • Previously issued remember-me tokens remain valid.

@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comments: 1

tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php: 1

438-438: Trivial · Maintainability & Code Quality · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Restore cart after both session-ID tests.

Both tests leave cart=kept in shared session storage. signout() does not remove it, and inherited teardown clears configuration and application context, not session contents. Native session storage references $_SESSION. (raw.githubusercontent.com)

Save the previous value and restore it in finally. If the key was absent, delete it instead. This prevents these fixtures from affecting later tests.

Based on learnings, tests that mutate shared global state must restore it even when an assertion throws.

Also applies to: 453-453

Source: Learnings

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 83c06d9 and 63ed47a.

Files selected for processing: 3

  • CHANGELOG.md
  • src/Auth/Adapters/SessionAuthAdapter.php
  • tests/Unit/Auth/Adapters/SessionAuthAdapterTest.php

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant